package com.cn.wanxi.servlet;

import com.cn.wanxi.service.impl.CookStyleService;
import com.cn.wanxi.service.impl.ProductService;
import net.sf.json.JSONArray;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@WebServlet("/product")
public class Product extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ProductService productService = new ProductService();
        //得到所有菜系
        CookStyleService cookStyleService = new CookStyleService();
        List<Map> listCookStyle = cookStyleService.getList("");
        //新建一个总的list集合
        List<Map> list = new ArrayList<>();
        for (int i = 0; i < listCookStyle.size(); i++) {
            Map<String, Object> map = new HashMap<>();
            List<Map> listProduct = productService.getProduct(" where a.cookstyleid=" + listCookStyle.get(i).get("id"));
            map.put("name", listCookStyle.get(i).get("name").toString());
            map.put("list", listProduct);
            list.add(map);
        }
        JSONArray jsonArray = JSONArray.fromObject(list);
        System.out.println(jsonArray.toString());
        resp.getWriter().println(jsonArray.toString());
    }
}
